package com.rzcoding.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.rzcoding.dto.base.ResultBean;
import com.rzcoding.dto.base.ResultConst;
import com.rzcoding.entity.Order;
import com.rzcoding.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping(value = "/orders" )
    public ResponseEntity findPage(@RequestParam(name = "query", required = false) String orderNum,
                                   @RequestParam(name = "pagenum",required = false,defaultValue = "1") Integer pageNum,
                                   @RequestParam(name = "pagesize",required = false,defaultValue = "10") Integer pageSize){

        PageInfo<Order> pageInfo = orderService.findPage(orderNum, pageNum, pageSize);
        JSONObject resp = new JSONObject().fluentPut("total", pageInfo.getTotal());
        resp.put("goods", pageInfo.getList());
        return new ResponseEntity<>(ResultBean.ok(ResultConst.GET_ORDERLIST_SUCC, resp), HttpStatus.OK);
    }

    @GetMapping(value = "/kuaidi/{id}" )
    public ResponseEntity getLogistics(@PathVariable String id){
        return new ResponseEntity<>(ResultBean.ok(ResultConst.GET_LOGISTICS_SUCC, orderService.findLogistics()),
                HttpStatus.OK);
    }

    @GetMapping(value = "/reports/type/{id}")
    public ResponseEntity getReports(@PathVariable Integer id){
        String reports = "{\"legend\":{\"data\":[\"华东\",\"华南\",\"华北\",\"西部\",\"其他\"]},\"yAxis\":[{\"type\":\"value\"}],\"xAxis\":[{\"data\":[\"2017-12-27\",\"2017-12-28\",\"2017-12-29\",\"2017-12-30\",\"2017-12-31\",\"2018-1-1\"]}],\"series\":[{\"name\":\"华东\",\"type\":\"line\",\"stack\":\"总量\",\"areaStyle\":{\"normal\":{}},\"data\":[2999,3111,4100,3565,3528,6000]},{\"name\":\"华南\",\"type\":\"line\",\"stack\":\"总量\",\"areaStyle\":{\"normal\":{}},\"data\":[5090,2500,3400,6000,6400,7800]},{\"name\":\"华北\",\"type\":\"line\",\"stack\":\"总量\",\"areaStyle\":{\"normal\":{}},\"data\":[6888,4000,8010,12321,13928,12984]},{\"name\":\"西部\",\"type\":\"line\",\"stack\":\"总量\",\"areaStyle\":{\"normal\":{}},\"data\":[9991,4130,7777,12903,13098,14028]},{\"name\":\"其他\",\"type\":\"line\",\"stack\":\"总量\",\"areaStyle\":{\"normal\":{}},\"data\":[15212,5800,10241,14821,15982,14091]}]}";
        return new ResponseEntity<>(ResultBean.ok(ResultConst.GET_REPORTS_SUCC, JSON.parseObject(reports,JSONObject.class)),
                HttpStatus.OK);
    }

}
